<?php
namespace Ekadmin\Controller;

use \Think\Controller;
use Think\Page;

class XiaoquController extends CommonController
{

    public function index()
    {
        $where['uniacid'] = $this->uniacid;
        $this->info = M("quyu")->field('id,name')
            ->order("sort")->where($where)
            ->select();
        
        $obj = M('xiaoqu');
        $param = I('get.');
        if ($param['status']) {
            $where['status'] = $param['status'];
        }
        if ($param['quyu_id']) {
            $where['quyu_id'] = $param['quyu_id'];
        }
        $this->quyu_id = intval($param['quyu_id']);
        if ($param['name']) {
            $where['name'] = array(
                'like',
                '%' . $param['name'] . '%'
            );
        }
        if ($param['tuan_name']) {
            $where['tuan_name'] = array(
                'like',
                '%' . $param['tuan_name'] . '%'
            );
        }
        if ($param['mobile']) {
            $where['mobile'] = array(
                'like',
                '%' . $param['mobile'] . '%'
            );
        }
        if ($param['tuan_nickname']) {
            $where['tuan_nickname'] = array(
                'like',
                '%' . $param['tuan_nickname'] . '%'
            );
        }
        $count = $obj->where($where)->count();
        $page = new Page($count);
        $list = $obj->where($where)
            ->limit($page->firstRow . ',' . $page->listRows)
            ->order('sort')
            ->select();
        if ($list) {
            for ($i = 0; $i < count($list); $i ++) {
                $rs = M("quyu")->find($list[$i]["quyu_id"]);
                if ($rs) {
                    $list[$i]["quyu_id"] = $rs["name"];
                } else {
                    $list[$i]["quyu_id"] = "无";
                }
            }
        }
        $this->assign('page', $page->show());
        $this->assign('count', $count);
        $this->assign('list', $list);
        $this->display();
    }



    public function edit()
    {
        $data = M('xiaoqu')->where("id=".I("id")." and uniacid=".$this->uniacid)->find();
        $this->assign('data', $data);
        $this->assign('list', $this->getQuyu());
        $this->display('form');
    }

    private function getQuyu()
    {
        return M('quyu')->where("uniacid=".$this->uniacid)->getField('id,name');
    }

    public function add()
    {
        $this->assign('list', $this->getQuyu());
        $this->display('form');
    }

    public function update()
    {
        if (IS_POST) {
            $param = I('post.');
          	
            if ($param['id']) {
                $rs = M("xiaoqu")->where("name='" . I('name') . "' and id!=" . $param['id'] . " and quyu_id=" . $param["quyu_id"]." and uniacid=".$this->uniacid)->find();
                if ($rs) {
                    ajax_return(1, "小区已存在");
                }
            } else {
                $rs = M("xiaoqu")->where("name='" . I('name') . "'  and quyu_id=" . $param["quyu_id"]." and uniacid=".$this->uniacid)->find();
                if ($rs) {
                    ajax_return(1, "小区已存在");
                }
            }
            if (intval($param['uid'])) {
                $rs = M('user')->where("id=" . $param['uid']." and uniacid=".$this->uniacid)->find();
                if (! $rs) {
                    ajax_return(1, "团长不存在");
                }
                if (! empty($rs["mobile"])) {
                    $param["mobile"] = $rs["mobile"];
                }
                if (! empty($rs["name"])) {
                    $param["tuan_name"] = $rs["name"];
                }
                $param["tuan_nickname"] = $rs["nickname"];
                M("user")->where("id=" . $param["uid"]." and uniacid=".$this->uniacid)->setField("is_tuan", 2);
            }
            $param['uid'] = intval($param['uid']);
            $param["tuan_nickname"]=$param["tuan_nickname"];
            $obj = M('xiaoqu');
            if ($param['id']) {
                $obj->save($param);
            } else {
                $param['addtime'] = time();
                $param['uniacid'] = $this->uniacid;
                $obj->add($param);
            }
            ajax_return(0, '保存成功', U('index'));
        }
    }

    public function del()
    {
        if (IS_POST) {
            $param = I('id');
            if (is_array($param)) {
                $map['id'] = array(
                    'in',
                    $param
                );
            } else {
                $map['id'] = $param;
            }
            
            $map["uniacid"]=$this->uniacid;
            
            M('xiaoqu')->where($map)->delete();
            
            ajax_return(0, '删除成功');
        }
    }

    /**
     * *@读取所有会员记录
     */
    public function user()
    {
        $param = I('get.');
        $where['uniacid'] = $this->uniacid;
        if ($param['nickname']) {
            $where['nickname'] = array(
                'like',
                '%' . $param['nickname'] . '%'
            );
        }
        $user = M('user');
        $count = $user->where($where)->count();
        $page = new Page($count);
        $list = $user->where($where)
            ->limit($page->firstRow . ',' . $page->listRows)
            ->select();
        $this->assign('page', $page->show());
        $this->assign('count', $count);
        $this->assign('list', $list);
        
        $this->display();
    }

    /**
     * *@获取团长Id
     */
    public function gettuanid()
    {}

    /**
     * *@导出 小区数据
     */
    public function exportorder()
    {
        $param = I('get.');
        $where['uniacid'] = $this->uniacid;
        if ($param['status']) {
            $where['status'] = $param['status'];
        }
        if ($param['quyu_id']) {
            $where['quyu_id'] = $param['quyu_id'];
        }
        if ($param['name']) {
            $where['name'] = array(
                'like',
                '%' . $param['name'] . '%'
            );
        }
        $obj = M('xiaoqu');
        $xlsCell = array(
            [
                'name',
                '小区名称'
            ],
            [
                'quyu_id',
                '线路'
            ],
            [
                'shopname',
                '自提点名称'
            ],
            [
                'address',
                '小区地址'
            ],
            [
                'lng',
                '纬度'
            ],
            [
                'lat',
                '经度'
            ],
            [
                'status',
                '状态'
            ],
            [
                'uid',
                '团长名称'
            ],
            [
                'peisong_type',
                '提货方式'
            ],
            [
                'peisong_jiner',
                '配送费'
            ],
            [
                'mobile',
                '团长手机号'
            ],
            [
                'tuan_name',
                '团长姓名'
            ],
            [
                'machine_code',
                '机器码'
            ],
            [
                'is_flag',
                '是否加入总团长'
            ]
        );
        $data = $obj->where($where)
            ->order('id desc')
            ->select();
        if ($data) {
            for ($i = 0; $i < count($data); $i ++) {
                $rs = M("quyu")->where("uniacid=".$this->uniacid." and id=".$data[$i]["quyu_id"])->find();
                if ($rs) {
                    $data[$i]["quyu_id"] = $rs["name"];
                } else {
                    $data[$i]["quyu_id"] = "无";
                }
                $data[$i]["status"] = $data[$i]["status"] == 1 ? '正常' : '冻结';
                $data[$i]["tuan_name"] .= "|团长Id:" . $data[$i]["uid"];
                $data[$i]["peisong_type"] = $data[$i]["peisong_type"] == 1 ? '自提' : '送货上门';
                $data[$i]["is_flag"] = $data[$i]["is_flag"] == 1 ? '没有加入总团长' : '加入总团长';
            }
        }
        if ($data) {
            $title = date("YmdHis") . "期间小区名单";
            exportExcel($title, $xlsCell, $data);
        } else {
            echo "<script>alert('没有记录');</script>";
            exit();
        }
    }
}